package concurrent;

/**
 * notify和notifyAll的区别在于一个激活所有wait的线程,另外1个只激活一个wait的线程.
 * 下面那个例子如果有3个线程在wait,main是notify的话程序是不会终止的,因为还有2个线程在wait
 * 如果是notifyall的话程序可以终止,因为3个线程都被激活了. 但是即使是3个线程都被notifyall激活了.同一时间也只有1个线程能获得锁
 * 
 * @author jyzjyz12@163.com
 * @since 2017年2月23日 上午9:20:17
 */
public class NotifyTest1 implements Runnable {
    Object o = new Object();

    public static void main(String[] args) throws InterruptedException {
        NotifyTest1 t = new NotifyTest1();
        Thread t1 = new Thread(t);
        Thread t2 = new Thread(t);
        Thread t3 = new Thread(t);
        t1.start();
        t2.start();
        t3.start();
        Thread.sleep(5000);
        synchronized (t) {
            t.notify();
        }
    }

    @Override
    public void run() {
        try {
            fun();
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    public synchronized void fun() throws InterruptedException {
        System.out.println(Thread.currentThread().getName() + "before");
        wait();
        System.out.println(Thread.currentThread().getName() + "after");
        System.out.println("========================");
    }
}
